<?php
namespace app\common\logic;
use think\facade\Db;

//引入支付宝充值sdk
use aop\AopClient;
use aop\request\AlipayTradePagePayRequest;
use aop\request\AlipayTradeQueryRequest;

class AliPayLogic{
	public static $init;
	static function init(){
		if (!isset(self::$init)) {//判断是否以前创建了当前类的实例
            $c = __CLASS__;//获取类名
            self::$init = new static;//如果没有创建，实例化当前类，这里实现类只实例化一次
        }
        return self::$init;//返回类的实例
	}


	//网页支付
	function webPay($data=[]){
		if(!$data){
			return false;
		}
		//获取支付宝配置
		$aliConfig = $this->getConfig();
        $aop =new AopClient();
        $aop->gatewayUrl =$aliConfig['gatewayUrl'];;
        $aop->appId =$aliConfig['app_id'];
        $aop->method ='alipayTradePagePay';
        $aop->charset ='utf-8';
        $aop->signType ='RSA2';
        $aop->alipayrsaPublicKey =$aliConfig['public_key'];
        $aop->rsaPrivateKey      =$aliConfig['merchant_private_key'];
        $biz_content = json_encode([
            'out_trade_no'=> $data['trade_number'],
            'product_code'=>'FAST_INSTANT_TRADE_PAY',
            'total_amount'=> $data['recharge_amount'],
            'subject'=>"用户账户充值，支付金额".$data['recharge_amount']."元。",
            'timeout_express'=>'30m',
        ]);
        $payRequest=new AlipayTradePagePayRequest();
        $payRequest->setReturnUrl($aliConfig['return_url']);
        $payRequest->setNotifyUrl($aliConfig['notify_url']);
        $payRequest->setBizContent($biz_content);
        $response =$aop->sdkExecute($payRequest);
        //返回支付路径
        return 'https://openapi.alipay.com/gateway.do?'.$response;
	}


	//查询订单是否支付成功
	function queryPay($order_sn=''){
		if(!$order_sn){
			return false;
		}
		$info  = Db::name('order_recharge')
					->field('id, trade_number, recharge_sn, recharge_amount')
					->where('recharge_sn', $order_sn)
					->find();
		if(!$info){
			return false;
		}

		$trade_number = $info['trade_number'];
		//获取支付宝配置
		$aliConfig = $this->getConfig();
		$aop = new AopClient ();
		$aop->gatewayUrl =$aliConfig['gatewayUrl'];;
        $aop->appId =$aliConfig['app_id'];
        $aop->apiVersion = '1.0';
        // $aop->method ='alipayTradeQuery';
        $aop->charset ='utf-8';
        $aop->signType ='RSA2';
		$aop->format='json';

        $aop->alipayrsaPublicKey =$aliConfig['public_key'];
        $aop->rsaPrivateKey      =$aliConfig['merchant_private_key'];
		$queryRequest = new AlipayTradeQueryRequest();

		$biz_content = json_encode([
            'out_trade_no'=> $trade_number
        ]);
		$queryRequest->setBizContent($biz_content);
		$result = $aop->execute($queryRequest); 

		$alipay_trade_query_response = $result->alipay_trade_query_response;

		if(!empty($alipay_trade_query_response)&&$alipay_trade_query_response->code == 10000){
			return $alipay_trade_query_response;
		} else {
			return false;
		}
	}
	//获取配置
	function getConfig(){
		$list = Db::name('config')->field('name,value')->where('type',3)->select()->toArray();
		$aliConfig = [];
		foreach($list as $v){
			if($v['name']=='alipay_appId'){
				$aliConfig['app_id'] = $v['value'];
			}

			if($v['name']=='alipay_notify_url'){
				$aliConfig['notify_url'] = $v['value'];
			}

			if($v['name']=='alipay_return_url'){
				$aliConfig['return_url'] = $v['value'];
			}

			if($v['name']=='alipay_gateway_url'){
				$aliConfig['gatewayUrl'] = $v['value'];
			}

			if($v['name']=='alipay_private_key'){
				$aliConfig['merchant_private_key'] = $v['value'];
			}

			if($v['name']=='alipay_public_key'){
				$aliConfig['public_key'] = $v['value'];
			}
		}
		return $aliConfig;
	}
}